#include "Stack.h"

void STInit(ST& rs, int n)
{
	rs.arr = (STDatatype*)malloc(sizeof(STDatatype) * n);
	rs.capacity = n;
	rs.top = 0;
}

void STPush(ST& rs, STDatatype x)
{
	//assert(st);
	if (rs.capacity == rs.top)
	{
		int newcapacity = rs.capacity == 0 ? sizeof(STDatatype) : rs.capacity * 2;
		STDatatype* ret = (STDatatype*)realloc(rs.arr, sizeof(STDatatype) * newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		rs.arr = ret;
		rs.capacity = newcapacity;
	}
	rs.arr[rs.top] = x;
	rs.top++;
}

int& STtop(ST& rs)
{
	return rs.arr[rs.top - 1];
}

void STDstory(ST& rs)
{
	if (rs.arr)
	{
		free(rs.arr);
		rs.arr = NULL;
	}
	rs.capacity = rs.top = 0;
}